<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/7
 * Time: 1:51
 */

namespace app\api\controller\v9;

use app\api\controller\v2\Auth;
use app\common\model\Article;
use app\common\model\ArticleCommentReply;
use app\common\model\MessageCenter;
use app\common\model\Recode;
use app\common\model\ZlAstr;
use think\Db;

/**个人中心相关接口
 * @title
 * @desc
 * @url
 * @version 2.0.0
 * Class Demo
 * @package app\api\controller\v9
 */
class Personal extends Auth
{
    /**
     * @title
     * @method GET
     * @desc  获取我的信息
     * @return array
     * @throws \app\common\lib\exception\ApiException
     */
    public function index()
    {
        //判断用户是否绑定准了号
        $uen = (new \app\common\model\User())->check_uen($this->user);
        $RecodeModel = new Recode();
        $MsgModel = new MessageCenter();
        $uid = $this->user['id'];
        //        将用户头像和档案头像统一
        $map = ['uid' => $this->user['id'], 'isself' => 1];
        $rea = $RecodeModel->getSelfRecode($map);
        //若用户表无头像或和档案头像不统一
        if ($rea['avatar'])
        {
            if ((!$this->user['avatar'] || $this->user['avatar'] == "") || ((strcmp($rea['avatar'], $this->user['avatar']) !== 0)))
            {
                try
                {
                    Db::table('app_user')->where('id', $this->user['id'])->update(['avatar' => $rea['avatar']]);
                }
                catch (\Exception $e)
                {
                    \think\Log::write('个人中心用户头像统一失败 ： ' . $e->getMessage(), 'error', true);
                }
            }
        }

        //若档案没有头像
        if ($this->user['avatar'])
        {
            if (!$rea['avatar'] || $rea['avatar'] == "")
            {
                try
                {
                    Db::table('app_recode')->where($map)->update(['avatar' => $this->user['avatar']]);
                }
                catch (\Exception $e)
                {
                    \think\Log::write('个人中心档案头像统一失败 ： ' . $e->getMessage(), 'error', true);
                }
            }
        }

        $recode = $RecodeModel->getRecodeWithAstro(['id' => $rea['id']]);
        $sex = $this->user['sex'] ?: 1;

        // 获取收藏的文章
        $collectData = (new Article())->getUserArticleCollect(['ucid' => $this->user['uc_id'], 'uid' => $this->user['id']]);

        $collectNum = count(array_filter(explode(',', $collectData)));

        $data = [
            'id' => (int)$recode['id'],
            'uen' => $uen ?: $this->user['uen'],
            'name' => $recode['name'],
            'sex' => (int)$sex,
            'avatar' => $recode['avatar'],
            'sun' => $recode['sun'],
            'moon' => $recode['moon'],
            'asc' => $recode['asc'],
            'is_astr' => 0,
            'is_zxdr' => 0,
            'is_inviter' => 0,
            'attention_num' => 0,
            'fans_num' => 0,
            'collect_num' => $collectNum < 0 ? 0 : $collectNum,
            'astr_level' => 0,
        ];

        //获取用户全部未读系统消息id
        $system_msg_id = $MsgModel->getAllUserMessage(['uid' => $uid, 'type' => 1]);
        //获取用户全部未读服务消息id
        $server_msg_id = $MsgModel->getAllUserMessage(['uid' => $uid, 'type' => 2]);
        $msg_count = count($system_msg_id) + count($server_msg_id);
        // 获取回复的阅读情况
        $replyModel = new ArticleCommentReply();
        $unread_ids = $replyModel->getUnReadReply(['ucid' => $this->user['uc_id'], 'uid' => $this->user['id']]);
        if ($unread_ids)
        {
            $reply_count = count($unread_ids);
        } else
        {
            $reply_count = 0;
        }
        $array1['msg_count'] = $msg_count + $reply_count;

        if (isset($this->user['auid']) && $this->user['auid'])
        {
            $astrInfo = (new ZlAstr())->getInfoById($this->user['auid']);

            $data['name'] = $astrInfo['nickname'] ?? $astrInfo['nickname'] ?: $data['name'];
            $data['is_astr'] = (int)$astrInfo['is_astr'];
            $data['is_zxdr'] = (int)$astrInfo['is_zxdr'];
            $data['is_inviter'] = (int)$astrInfo['is_inviter'];
            $data['attention_num'] = (int)$astrInfo['attention_num'];
            $data['fans_num'] = (int)$astrInfo['fans_num'];
            $data['astr_level'] = (int)$astrInfo['astr_level'];
        }

        $array = array_merge($data, $array1);
        return show(200, '成功', $array);
    }
}
